package com.pyg.goods.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import com.pyg.pojo.TbGoodsExample;
import com.pyg.pojo.TbGoodsExample.Criteria;
import com.pyg.mapper.TbGoodsMapper;
import com.pyg.pojo.TbGoods;
import com.pyg.goods.service.TbGoodsService;

import com.pyg.utils.PygStringUtils;
import entity.PageResult;

/**	*  @Author zhuliangcai
*  @Email zhuliangcai@itcast.cn
*  @Date 2018-04-03 20:07:45
*/
@Service
public class TbGoodsServiceImpl implements TbGoodsService {
	@Autowired
	private TbGoodsMapper tbGoodsMapper;

	@Override
	public List<TbGoods> findAll() {
		return tbGoodsMapper.selectByExample(null);
	}

	@Override
	public PageResult findPage(int pageNum, int pageSize) {
		// 开启分页插件功能
		PageHelper.startPage(pageNum, pageSize);

		Page<TbGoods> page = (Page<TbGoods>) tbGoodsMapper.selectByExample(null);

		return new PageResult(page.getTotal(), page.getResult());
	}

	/**
	 * 品牌条件分页
	 *
	 * @param TbGoods
	 *            查询条件
	 * @param pageNum
	 *            当前页面
	 * @param pageSize
	 *            每页记录数
	 * @return
	 */
	@Override
	public PageResult findPage(TbGoods tbGoods, int pageNum, int pageSize) {
		// 开启分页插件功能
		PageHelper.startPage(pageNum, pageSize);

		TbGoodsExample example = null;
		if (tbGoods != null) {
			example = new TbGoodsExample();
			Criteria criteria = example.createCriteria();
			if (PygStringUtils.isNotEmpty(tbGoods.getSellerId())) {
				criteria.andSellerIdLike("%" + tbGoods.getSellerId() + "%");
			}
			if (PygStringUtils.isNotEmpty(tbGoods.getGoodsName())) {
				criteria.andGoodsNameLike("%" + tbGoods.getGoodsName() + "%");
			}
			if (PygStringUtils.isNotEmpty(tbGoods.getDefaultItemId())) {
				criteria.andDefaultItemIdEqualTo(tbGoods.getDefaultItemId() );
			}
			if (PygStringUtils.isNotEmpty(tbGoods.getAuditStatus())) {
				criteria.andAuditStatusLike("%" + tbGoods.getAuditStatus() + "%");
			}
			if (PygStringUtils.isNotEmpty(tbGoods.getIsMarketable())) {
				criteria.andIsMarketableLike("%" + tbGoods.getIsMarketable() + "%");
			}
			if (PygStringUtils.isNotEmpty(tbGoods.getBrandId())) {
				criteria.andBrandIdEqualTo(tbGoods.getBrandId() );
			}
			if (PygStringUtils.isNotEmpty(tbGoods.getCaption())) {
				criteria.andCaptionLike("%" + tbGoods.getCaption() + "%");
			}
			if (PygStringUtils.isNotEmpty(tbGoods.getCategory1Id())) {
				criteria.andCategory1IdEqualTo(tbGoods.getCategory1Id() );
			}
			if (PygStringUtils.isNotEmpty(tbGoods.getCategory2Id())) {
				criteria.andCategory2IdEqualTo(tbGoods.getCategory2Id() );
			}
			if (PygStringUtils.isNotEmpty(tbGoods.getCategory3Id())) {
				criteria.andCategory3IdEqualTo(tbGoods.getCategory3Id() );
			}
			if (PygStringUtils.isNotEmpty(tbGoods.getSmallPic())) {
				criteria.andSmallPicLike("%" + tbGoods.getSmallPic() + "%");
			}
			if (PygStringUtils.isNotEmpty(tbGoods.getPrice())) {
				criteria.andPriceEqualTo(tbGoods.getPrice() );
			}
			if (PygStringUtils.isNotEmpty(tbGoods.getTypeTemplateId())) {
				criteria.andTypeTemplateIdEqualTo(tbGoods.getTypeTemplateId() );
			}
			if (PygStringUtils.isNotEmpty(tbGoods.getIsEnableSpec())) {
				criteria.andIsEnableSpecLike("%" + tbGoods.getIsEnableSpec() + "%");
			}
			if (PygStringUtils.isNotEmpty(tbGoods.getIsDelete())) {
				criteria.andIsDeleteLike("%" + tbGoods.getIsDelete() + "%");
			}
		}

		Page<TbGoods> page = (Page<TbGoods>) tbGoodsMapper.selectByExample(example);

		return new PageResult(page.getTotal(), page.getResult());
	}

	@Override
	public void add(TbGoods tbGoods) {
		tbGoodsMapper.insert(tbGoods);
	}

	@Override
	public void del(Long[] ids) {
		for (Long id : ids) {
			tbGoodsMapper.deleteByPrimaryKey(id);
		}
	}

	@Override
	public TbGoods findOne(Long id) {
		return tbGoodsMapper.selectByPrimaryKey(id);
	}

	@Override
	public void update(TbGoods tbGoods) {
		tbGoodsMapper.updateByPrimaryKey(tbGoods);
	}

}
